# coding:utf-8
"""
description:My account
"""
import os

from pages.Sidebar_page import sidebar_page
from appium.webdriver.common import mobileby
import random

from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML

testElements = get_yaml(os.path.join(TEST_Element_YAML, "MyAccount.yaml"))


class myaccount_page(sidebar_page):
    by = mobileby.MobileBy()

    btnBack_loc = (by.ID, testElements.get_elementInfo(0))

    def click_btnBack(self):
        self.find_element(*self.btnBack_loc).click()

    """
    profile picture
    """
    MemberSettingsPic_loc = (by.ID, testElements.get_elementInfo(1))
    Pics_loc = (by.ANDROID_UIAUTOMATOR, testElements.get_elementInfo(2))

    def click_btnMemberSettingsPic(self):
        """点击My account page中头像"""
        self.find_element(*self.MemberSettingsPic_loc).click()

    def getAndClick_Pics(self):
        """随意获取手机recent相册（后续在选择手机capture+下面的图片），并点击"""
        if self.assertVisible(*self.Pics_loc):
            LOG.error("随机选择手机recent相册图片。。。")
            random.choice(self.find_elements(*self.Pics_loc)).click()
        else:
            LOG.error("手机recent相册没有图片，后续功能将不会成功执行。。。")

    def func_ChangePic(self, message):
        """
        Myaccount 更换头像
        :param message:
        :return:
        """
        super().click_homeicon()  # 点击dashboard页面的左上角三杆
        super().click_btnHeaderProfilePic()  # 点击side bar页面的左上角头像框
        self.click_btnMemberSettingsPic()
        self.getAndClick_Pics()
        return self.find_toast(message)

    """change password"""
    viewChangepwd_loc = (by.XPATH, testElements.get_elementInfo(3))
    etChangePwOldPw_loc = (by.ID, testElements.get_elementInfo(4))
    etChangePwNewPw_loc = (by.ID, testElements.get_elementInfo(5))
    etChangePwCfmPw_loc = (by.ID, testElements.get_elementInfo(6))
    btnChangePw_loc = (by.ID, testElements.get_elementInfo(7))

    def click_viewChangepwd(self):
        """点击Change Pwd一行，打开change pwd弹框"""
        self.find_element(*self.viewChangepwd_loc).click()

    def input_ChangePwOldPw(self, ChangePwOldPw):
        """输入当前密码"""
        self.send_keys(ChangePwOldPw, self.find_element(*self.etChangePwOldPw_loc))

    def input_ChangePwNewPw(self, ChangePwNewPw):
        """输入新密码"""
        self.send_keys(ChangePwNewPw, self.find_element(*self.etChangePwNewPw_loc))

    def input_ChangePwCfmPw(self, ChangePwCfmPw):
        """输入新密码confirm"""
        self.send_keys(ChangePwCfmPw, self.find_element(*self.etChangePwCfmPw_loc))

    def click_btnChangePw(self):
        """点击Change Pwd按钮"""
        self.find_element(*self.btnChangePw_loc).click()

    def assertVisible_btnChangePw(self):
        """判断btnChangePw按钮是否存在"""
        return self.assertVisible(*self.btnChangePw_loc)

    def func_changepwdSuccess(self, OldPw, NewPw, CfmPw, message):
        """
        change pwd,change pwd success
        :param OldPw:
        :param NewPw:
        :param CfmPw:
        :param message:
        :return:
        """
        if OldPw == "" or NewPw == "":
            self.click_viewChangepwd()  # 点击Change Pwd一行，打开change pwd弹框
        self.input_ChangePwOldPw(OldPw)
        self.input_ChangePwNewPw(NewPw)
        self.input_ChangePwCfmPw(CfmPw)
        self.click_btnChangePw()
        return self.find_toast(message)

    """删除账号"""
    delAccbtn_loc = (by.CLASS_NAME, testElements.get_elementInfo(8))
    btnClosePopup_loc = (by.ID, testElements.get_elementInfo(9))
    btnDeletePopupDelete_loc = (by.ID, testElements.get_elementInfo(10))
    btnDeletePopupCancel_loc = (by.ID, testElements.get_elementInfo(11))

    def click_btndelAccbtn(self):
        """点击delete account按钮"""
        self.find_element(*self.delAccbtn_loc).click()

    def click_btnClosePopup(self):
        """点击delete account 弹框x按钮"""
        self.find_element(*self.btnClosePopup_loc).click()

    def click_btnDeletePopupDelete(self):
        """点击弹框delete按钮"""
        self.find_element(*self.btnDeletePopupDelete_loc).click()

    def click_btnDeletePopupCancel(self):
        """点击弹框cancel按钮"""
        self.find_element(*self.btnDeletePopupCancel_loc).click()

    def assertVisible_btnDelete(self):
        """判断btn delete按钮是否存在"""
        return self.assertVisible(*self.delAccbtn_loc)

    def func_delAccCancel(self):
        """
        Delete Account取消,断言delete按钮存在
        :return:
        """
        self.click_btndelAccbtn()
        self.click_btnDeletePopupCancel()
        return self.assertVisible_btnDelete()

    def func_DeleteAccount(self, message):
        """
        删除成功，断言提示语
        :param message:
        :return:
        """
        self.click_btndelAccbtn()
        self.click_btnDeletePopupDelete()
        return self.find_toast(message)
